package com.alexsun.smarthome

import android.content.Intent
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.IOException

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private var usernameEditor: EditText? = null
    private var passwordEditor: EditText? = null
    private val BASE_URL = "http://139.196.222.129:8081"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        usernameEditor = findViewById(R.id.username_edit)
        passwordEditor = findViewById(R.id.password_edit)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.login_btn -> {
                // TODO 删除
                val intent = Intent()
                intent.setClass(this@MainActivity, ControlActivity::class.java)
                startActivity(intent)
                // 验证登录
                val json = JSONObject()
                json.put("username", usernameEditor!!.text.toString())
                json.put("password", passwordEditor!!.text.toString())
                val body = json.toString().toRequestBody("application/json".toMediaTypeOrNull());
                val request = Request.Builder()
                    .url("$BASE_URL/users/login")
                    .method("POST", body)
                    .build()

                OkHttpClient().newCall(request).enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        Looper.prepare();
                        Toast.makeText(this@MainActivity, "请求失败", Toast.LENGTH_SHORT).show()
                        Log.e("POST", e.toString())
                        Looper.loop();
                    }
                    override fun onResponse(call: Call, response: Response) {
                        val responseJson = JSONObject(response.body?.string() ?: "")
                        val code = responseJson["code"]
                        val intent = Intent()
                        intent.setClass(this@MainActivity, ControlActivity::class.java)
                        if (code == 0) {
                            Looper.prepare();
                            Toast.makeText(this@MainActivity, "登录成功", Toast.LENGTH_SHORT).show()
                            startActivity(intent)
                            Looper.loop();
                        } else {
                            Looper.prepare();
                            Toast.makeText(this@MainActivity, "用户名或密码错误", Toast.LENGTH_SHORT).show()
                            Looper.loop();
                        }
                    }
                })
            }
            R.id.register_btn -> {
                val intent = Intent()
                intent.setClass(this@MainActivity, RegisterActivity::class.java)
                startActivity(intent)
            }
        }
    }
}